package main.controller;

import java.util.List;

import javax.annotation.Resource;

import main.dao.FatherRepository;
import main.entity.Father;
import main.entity.Girl;
import main.service.GirlService;
import main.util.JudgeUtil;
import main.util.PageableTool;
import main.util.ResultData;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GirlController {

	private Logger LOG = LoggerFactory.getLogger(GirlController.class);
	
	@Autowired
	private GirlService girlService;
	
	@Resource
	private FatherRepository fatherRepository;
	
/*	*//**
	 * 插入一个女孩
	 * @return
	 *//*
	@RequestMapping(value="/addGirl",method=RequestMethod.POST)
	public ResultData InsertGirl(Girl girl) {
		LOG.debug("插入一个女朋友");
		girl.setCupSize(girl.getCupSize());
		girl.setAge(girl.getAge());
		girl.setName(girl.getName());
		girl.setBoyFriend(girl.getBoyFriend());
		girl.setFather(girl.getFather());
		return ResultData.generateSuccessResultData("成功", girlRepository.save(girl));
	}*/
	/**
	 * 添加一个父亲
	 * @param father
	 * @return
	 */
	@RequestMapping(value="/addFather",method=RequestMethod.POST)
	public ResultData InsertFather(Father father) {
		LOG.debug("添加一个父亲");
		if(JudgeUtil.isNull(father)) {
			return ResultData.generateFailResultData("父亲为空了");
		}
		father.setAge(father.getAge());
		father.setName(father.getName());
		return ResultData.generateSuccessResultData("yes", fatherRepository.save(father));
	} 
	
	/**
	 * 通过男朋友姓名来获取所有的女朋友		
	 * @param id
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/getMyGirl/{name}",method=RequestMethod.POST)
	public ResultData getMyGirlFriend(@PathVariable("name") String name,Pageable pageable) {
		LOG.debug("通过id查询所有的女朋友");
		
		if(StringUtils.isBlank(name)){
			return ResultData.generateFailResultData("男朋友姓名必传!");

		}
		//http://blog.csdn.net/u011930471/article/details/52156858
		Page<Girl> pageData = girlService.getGirlByBoyId(name,pageable);

		return ResultData.generateSuccessResultData("哇,收获了"+ String.valueOf(pageData.getContent().size()) +"个女朋友", pageData);
	}
	
	/**
	 * 通过男朋友姓名和父亲id来获取所有的女朋友		
	 * @param id
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/getMyGirl",method=RequestMethod.POST)
	public ResultData getMyGirl(@RequestParam("name") String boyFriendName,@RequestParam("id") Integer father_id,Integer page) {
		if(StringUtils.isBlank(boyFriendName)) {
			return ResultData.generateFailResultData("不认识男朋友");
		}
		
		if(JudgeUtil.isNull(father_id)) {
			return ResultData.generateFailResultData("没有父亲");
		}
		
		Page<Girl> pageData = girlService.getGirlByNameAndId(boyFriendName, father_id, page);
		
		return ResultData.generateSuccessResultData("哇,收获了"+ String.valueOf(pageData.getContent().size()) +"个女朋友",pageData);
	}
	
	/**
	 * 通过男朋友姓名来获取所有的女朋友		
	 * @param name
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/getMyGirls",method=RequestMethod.POST)
	public ResultData getGirlFriend(String name,Pageable pageable) {
		if(StringUtils.isBlank(name)) {
			return ResultData.generateFailResultData("不认识男朋友");
		}
		
		Page<Girl> page = girlService.getGirlByName(name, pageable);
		
		return ResultData.generateSuccessResultData("哇,收获了"+ String.valueOf(page.getContent().size()) +"个女朋友", page);
	}
	
	/**
	 * 通过映射获取
	 * @return
	 */
	@RequestMapping(value="/getGirls",method=RequestMethod.POST)
	public ResultData getGirl(String address) {
		LOG.debug("通过地址查询所有的女朋友");
		if(StringUtils.isBlank(address)) {
			return ResultData.generateFailResultData("男朋友没有房子不能拥有女票");
		}
		List<Girl> listData = girlService.getGirl(address);
		return ResultData.generateSuccessResultData("提取女票", listData);
	}
}
